package alfabeta;

import java.util.ArrayList;

/**
 * @author jacobbj
 *
 *
 *
 */
public class BoardIndex {

	private int[][] varIndex;
	int size;

	public BoardIndex(int size) {

		this.size = size;
		varIndex = new int[size][size];

		// inititalize varIndex
		int index = 0;
		for (int i=0; i< size ; i++ ) {
			for (int j=0; j< size; j++ ) {
				varIndex[i][j] = index;
				index++;
			}
		}	
	}

	// returns index of var at given board coordinate 
	public int getVarIndex(int col, int row ) {
		return varIndex[row-1][col-1];
	}

	// returns Arraylist with index of all vars in given column
	public ArrayList<Integer> varsInColumn(int col) {
		if (col>size) return null;
		else {
			ArrayList<Integer> vars = new ArrayList<Integer>();
			int index = (col-1);
			for (int j=0; j<size; j++) {
				vars.add(varIndex[j][index]);
			}
			return vars;
		}	
	}

	// returns Arraylist with index of all vars in given row
	public ArrayList<Integer> varsInRow(int row) {
		if (row>size) return null;
		else {
			ArrayList<Integer> vars = new ArrayList<Integer>();
			int index = (row-1);
			for (int j=0; j<size; j++) {
				vars.add(varIndex[index][j]);
			}
			return vars;
		}	
	}

	// return index in left diagonal
	public ArrayList<Integer> varsLeftDiagUp(int col, int row) {
		ArrayList<Integer> vars = new ArrayList<Integer>();

		if (row>size || col>size || row<1 || col<1 )  return null;

		else {

			int x = col;
			int y = row;

			while (x > 0 && y <= size ) {
				vars.add(getVarIndex(x, y));
				x--; 
				y++;
			}
		}

		return vars;
	}

	// return index in right diagonal
		public ArrayList<Integer> varsRightDiagUp(int col, int row) {
			ArrayList<Integer> vars = new ArrayList<Integer>();

			if (row>size || col>size || row<1 || col<1 )  return null;

			else {

				int x = col;
				int y = row;

				while (x <= size && y <= size ) {
					vars.add(getVarIndex(x, y));
					x++; 
					y++;
				}
			}

			return vars;
		}
	
	// returns Arraylist with index of all vars in odd directions
	public ArrayList<Integer> varsInOddDirections(int col, int row) {
		if (row>size || col>size) return null;
		else {
			ArrayList<Integer> vars = new ArrayList<Integer>();

			// collect north-east / south-west
			int startX = col-1;
			int startY = col-1;
			while (startX>0 && startY > 0 ) {
				vars.add(getVarIndex(startX, startY));
				startX--; startY--;
			}

			startX = col+1;
			startY = row+1;
			while (startX <= size && startY <= size ) {
				vars.add(getVarIndex(startX, startY));
				startX++; startY++;
			}

			// collect north-west / south-east
			startX = col+1;
			startY = col-1;
			while (startX <= size && startY > 0 ) {
				vars.add(getVarIndex(startX, startY));
				startX++; startY--;
			}

			startX = col-1;
			startY = row+1;
			while (startX >0 && startY <= size ) {
				vars.add(getVarIndex(startX, startY));
				startX--; startY++;
			}

			return vars;
		}	
	}

	// prints out boardIndexes
	public void printBoard() {

		for (int i=size; i>0; i-- ) {
			System.out.println(this.varsInRow(i));
		}
	}



}
